/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.Alumno;
import modelo.AsistenciaAlumno;
import modelo.Curso;
import modelo.DAO;

/**
 *
 * @author Fabian
 */
@WebServlet(name = "BuscarServlet", urlPatterns = {"/buscar.view"})
public class BuscarServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            DAO d=new DAO();
            String run=request.getParameter("cboBuscarAlumno");
            String run1=request.getParameter("cboAlumnoBuscar");
            String c=request.getParameter("cboCursoBuscar");
            List<AsistenciaAlumno> asisalumno;
            List<Alumno> alumnos=d.cargarAlumno();
            List<Curso> curso=d.cargarAsignatura();
            if(run != null){
                asisalumno=d.getAsistencias(run);
            }else if(run1 != null && c != null){
                asisalumno=d.getAsistencias(run1, c);
            }else{
                asisalumno=null;
            }
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet BuscarServlet</title>");
            out.println("<link rel='stylesheet' type='text/css' href='css/asistencia.css'>");
            out.println("</head>");
            out.println("<body>");
            out.println("<form action='buscar.view'>");
            out.println("<select name='cboBuscarAlumno'>");
            for (Alumno alumno2 : alumnos) {
                out.println("<option value='"+alumno2.getRut()+"'>"+alumno2.getNombre()+"</option>");
            }
            out.println("</select>");
            out.println("<input type='submit' value='Buscar Alumno'>");
            out.println("</form>");
            out.println("<form action='buscar.view'>");
            out.println("<select name='cboCursoBuscar'>");
            for (Curso curso1 : curso) {
                out.println("<option value='"+curso1.getId()+"'>"+curso1.getNombre()+"</option>");
            }
            out.println("</select>");
            out.println("<select name='cboAlumnoBuscar'>");
            for (Alumno alumno2 : alumnos) {
                out.println("<option value='"+alumno2.getRut()+"'>"+alumno2.getNombre()+"</option>");
            }
            out.println("</select>");
            out.println("<input type='submit' value='Buscar Alumno'>");
            out.println("</form>");
            out.println("<table class='zebra'>");
            out.println("<tr>");
            out.println("<td>ID</td>");
            out.println("<td>Run</td>");
            out.println("<td>Nombre</td>");
            out.println("<td>Apellido Paterno</td>");
            out.println("<td>Apellido Materno</td>");
            out.println("<td>Edad</td>");
            out.println("<td>Sexo</td>");
            out.println("<td>Fecha</td>");
            out.println("<td>Asisto</td>");
            out.println("<td>Curso</td>");
            out.println("</tr>");
            for (AsistenciaAlumno asis1 : asisalumno) {
                out.println("<tr>");
                out.println("<td>"+asis1.getAlumno().getId()+"</td>");
                out.println("<td>"+asis1.getAlumno().getRut()+"</td>");
                out.println("<td>"+asis1.getAlumno().getNombre()+"</td>");
                out.println("<td>"+asis1.getAlumno().getApellidoPaterno()+"</td>");
                out.println("<td>"+asis1.getAlumno().getApellidoMaterno()+"</td>");
                out.println("<td>"+asis1.getAlumno().getEdad()+"</td>");
                out.println("<td>"+asis1.getAlumno().getSexo()+"</td>");
                out.println("<td>"+asis1.getAsistencia().getFecha()+"</td>");
                if (asis1.getAsistencia().isAsistio() == true) {
                    out.println("<td>Si</td>");
                }else{
                    out.println("<td>No</td>");
                }
                out.println("<td>"+asis1.getCurso().getNombre()+"</td>");
                out.println("</tr>");
            }
            out.println("</table>");
            out.println("<a href='index.jsp'>Volver</a>");
            out.println("</body>");
            out.println("</html>");
        } catch (SQLException ex) {
            Logger.getLogger(BuscarServlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
